<?php

namespace App\Http\Controllers\Company\English;

use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Models\Common\English\EnCollArea;
use App\Models\Common\English\EnCollOption;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\ResumeFavorite;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FavoriteController extends CommonController
{

    /**
     * 收到应聘简历
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $user = auth()->guard('company')->user();
        //获取面试通知记录
        $relist = DB::table('resume_favorite as log')
            ->join('resume as t1', 'log.resume_id', '=', 't1.id')
            ->leftjoin('company_job as t2', 'log.job_id', '=', 't2.id')
            ->select(
                'log.id','log.status','log.updated_at','t1.name','t1.sex','t1.birthday',
                't1.education','t1.homeaddress','t2.job_title','t1.intentionjobs','t1.language','log.job_id','log.resume_id'
            )
            ->where('log.uid',$user->uid)
            ->where(function ($query) use ($request) {
                if(strlen($request->keyWord)>0){
                    $query->where('t2.job_title', 'like', '%'.$request->keyWord.'%')->orWhere('t1.name','like','%'.$request->keyWord.'%');
                }
            })
            ->where('log.status','!=',4)
            ->orderBy('log.updated_at','DESC')
            ->paginate(20);
        //print_r($queries );

        foreach($relist as $v) {
            if ($v->language == 'en') {
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = 'Man';
                        break;
                    case "2":
                        $v->cnsex = 'Woman';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = EnCollOption::getOptionName($v->education, 'education');
                $v->homeaddress = EnCollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }else{
                switch ($v->sex) {
                    case "1":
                        $v->cnsex = '男';
                        break;
                    case "2":
                        $v->cnsex = '女';
                        break;
                    default:
                        $v->cnsex = '';
                }
                $v->education = CollOption::getOptionName($v->education, 'education');
                $v->homeaddress = CollArea::getAreaName($v->homeaddress);
                $v->birthday = birthday($v->birthday);
            }
        }
        return Style::view('company.english.favoriteIndex',compact('relist'));
    }


    public function status(Request $request)
    {
        $allow=array(4); //允许的状态码
        if(in_array($request->status,$allow)) {
            $user = auth()->guard('company')->user();
            $input['status'] = $request->status;
            if(!empty($request->id)){
                ResumeFavorite::where('uid', $user->uid)->where('id', $request->id)->update($input);
            }elseif(is_array($request->selected_id)){
                ResumeFavorite::where('uid', $user->uid)->whereIn('id', $request->selected_id)->update($input);
            }

            return back()->withInput()->with('info',"Deleted!");
        }
    }
}